/* const beaeUrl = 'http://ylsb.xibuts.cn/api' */
// const beaeUrl = 'https://ylsb.westts.cn/api'  //服务器
import baseUrl from '@/request/baseUrl.js'
// const beaeUrl = 'https://ylsbnew.westts.cn/api'//测试地址

function request(method, url, data) {
	return new Promise((resolve, reject) => {
const token = uni.getStorageSync('userInfo').token
		uni.showLoading({
			title: '加载中'
		});
		uni.request({
			url: baseUrl + url,
			data,
			header: {
				token
			},
			method,
			success(res) {
				uni.hideLoading();
				// console.log(res.statusCode,'9',res.data)
				switch (res.statusCode) {
					case 200:
						if (res.data.code == 1) {
							resolve(res.data)
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 1500
							})
						}
						break;
					case 401:
						uni.showToast({
							title: res.data.msg,
							duration: 1500,
							icon: 'none',
							mask: true
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}, 1500)
						break;
						case 404:
							uni.showToast({
								title: '服务器错误',
								duration: 1500,
								icon: 'none'
							})
							break;
					case 500:
						uni.showToast({
							title: '服务器错误',
							duration: 1500,
							icon: 'none'
						})
						break;
				}
				/* if(res.code) {
				    resolve(res.data)
				}else{
				    uni.showModal({
				        title: '提示',
				        content: res.msg
				    })
				    resolve(false)
				} */
			},
			fail(err) {
				console.log(err, 23);
				uni.hideLoading();
				uni.showModal({
					title: '提示',
					content: '网络断开，稍后重试！'
				})
				resolve(false)
			}
		})
	})
}

module.exports = {
	"get": function(url, data) {
		return request("GET", url, data);
	},
	"post": function(url, data) {
		return request("POST", url, data);
	}
	/*  "put": function(url, data, header) {
	      return request("PUT", url, data, header);
	  },
	  "delete": function(url, data, header) {
	      return request("DELETE", url, data, header);
	  } */
}
